<?php

/**
 * 登录控制器.
 * User: Administrator
 * Date: 2017/10/18 0018
 * Time: 10:00
 */

namespace User\Controller;

use Think\Controller;

class UserController extends Controller
{
    /**
     * 登录
     */
    public function post_login()
    {
        if (IS_PSOT) {
            $phone = I('post.phone');
            $password = I('post.password');
            $j_push_id = I('post.j_push_id');
            /*if (empty($j_push_id)) {
                $this->ajaxReturn(['status' => 0, 'msg' => '请上传推送id！']);
            }*/
            $userLogin = D('UserLogin');
            $result = $userLogin->Login($phone, $password);
            if ($result['status'] != 1) {
                $this->ajaxReturn($result);
            }
            // 更新推送id
            /*M('User')->where(['user_id' => $result['msg']['user_id']])->setField('j_push_id', $j_push_id);*/
            $this->ajaxReturn($result);
        } else {
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        }
    }
    /**
     * 企业类别列表
     */
    public function get_type()
    {
        $typeM = M('type_list');
        $result = $typeM->select();
        if (empty($result)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        }
        $data['status']=1;
        $data['msg']=$result;
        $this->ajaxReturn($data);
       
    }
    /**
     * 企业注册
     */
    public function post_register()
    {
        if (IS_PSOT) {
            $postdata = I('post.');
            /*if (empty($j_push_id)) {
                $this->ajaxReturn(['status' => 0, 'msg' => '请上传推送id！']);
            }*/
            $companyData['name'] = $postdata['name'];
            $companyData['code'] = $postdata['code'];
            $companyData['capital'] = $postdata['capital'];
            $companyData['address'] = $postdata['address'];
            $companyData['type'] = $postdata['type'];
            $companyData['gps_position'] = $postdata['gps_position'];
            $companyData['person_liable'] = $postdata['person_liable'];
            $companyData['liable_phone'] = $postdata['liable_phone'];
            $companyData['safety_officer'] = $postdata['safety_officer'];
            $companyData['safety_officer_phone'] = $postdata['safety_officer_phone'];
            $companyData['account'] = $postdata['account'];
            $companyData['password'] = md5($postdata['password']);
            $companyData['danger_a'] = $postdata['danger_a'];
            $companyData['danger_b'] = $postdata['danger_b'];
            $companyData['danger_c'] = $postdata['danger_c'];
            $companyData['create_time'] = time();
            $companyData['other'] = $postdata['other'];
            $companyM = M('Company');
            $resultCompany = $companyM->add($companyData);

            $userData['username'] = $companyData['person_liable'];
            $userData['phone'] = $companyData['account'];
            $userData['password'] = $companyData['password'];
            $userData['level_id'] = 5;
            $userData['company_id'] = $resultCompany;

            $returns['company'] = $companyData;
            $returns['user'] = $userData;
            
            $userM = M('User');
            $resultUser = $userM->add($userData);

            $this->ajaxReturn(['status' => 1, 'msg' => $returns]);
            exit;
        } else {
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        }
    }
    /**
     * 企业修改
     */
    public function update_info()
    {
        if (IS_PSOT) {
            $postdata = I('post.');
            /*if (empty($j_push_id)) {
                $this->ajaxReturn(['status' => 0, 'msg' => '请上传推送id！']);
            }*/
            $whereUser['user_id'] = $postdata['user_id'];
            $company_id = $postdata['company_id'];
            if($whereUser['user_id']<=0){
                $this->ajaxReturn(['status' => 0, 'msg' => '缺少user_id']);
            }
            if(!empty($company_id)){
                $whereUser['company_id'] = $company_id;
            }
            $companyData['name'] = $postdata['name'];
            $companyData['code'] = $postdata['code'];
            $companyData['capital'] = $postdata['capital'];
            $companyData['address'] = $postdata['address'];
            $companyData['type'] = $postdata['type'];
            $companyData['gps_position'] = $postdata['gps_position'];
            $companyData['person_liable'] = $postdata['person_liable'];
            $companyData['liable_phone'] = $postdata['liable_phone'];
            $companyData['safety_officer'] = $postdata['safety_officer'];
            $companyData['safety_officer_phone'] = $postdata['safety_officer_phone'];
            //$companyData['account'] = $postdata['account'];
           // $companyData['password'] = md5($postdata['password']);
            $companyData['danger_a'] = $postdata['danger_a'];
            $companyData['danger_b'] = $postdata['danger_b'];
            $companyData['danger_c'] = $postdata['danger_c'];
            $companyData['other'] = $postdata['other'];
            $companyData['create_time'] = time();

            $userData['username'] = $companyData['person_liable'];
            //$userData['phone'] = $companyData['account'];
            //$userData['password'] = $companyData['password'];
            $userData['level_id'] = 5;
            //$userData['company_id'] = $resultCompany;

            $returns['company'] = $companyData;
            $returns['user'] = $userData;
            
            $userM = M('User');
            $findUser = $userM->where('user_id='.$whereUser['user_id'])->find();
            if(!$findUser){
                $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
            }
            

            $companyM = M('Company');
            if(!empty($company_id)){
                //执法者修改
                $resultCompany = $companyM->where('id='.$company_id)->save($companyData);
            }else{
                $resultUser = $userM->where($whereUser)->save($userData);
                $resultCompany = $companyM->where('id='.$findUser['company_id'])->save($companyData);
            }
            

            $this->ajaxReturn(['status' => 1, 'msg' => $returns]);
            exit;
        } else {
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        }
    }
    
    /**
     * 修改密码
     */
    public function post_change_password()
    {
        $user_id = I('post.user_id');
        $password = I('post.password');
        $new_password = I('post.new_password');
        if (empty($user_id)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        }
        if (empty($new_password)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '上传新密码！']);
        }
        $user_password = M('User')->where(['user_id' => $user_id])->getField('password');
        if ($user_password != md5($password)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '密码错误！']);
        }
        M('User')->where(['user_id' => $user_id])->setField('password', md5($new_password));
        $this->ajaxReturn(['status' => 1, 'msg' => '修改成功！']);
    }
	
	public function post_worning_send()
	{
		$user_id = I('post.user_id');
		$username = M('User')->where(['user_id'=>$user_id])->getField('username');
		$danger_name = I('post.danger_name');
		$parent1 = M('User')->where(['user_id'=>$user_id])->getField('parent_id');
		if ($parent1 != 0) {
			$user1 = M('User')->where(['user_id'=>$parent1])->find();
			send_sms($user1['phone'], '【' . $danger_name .'】风险点存在重要安全隐患，上报人【' . $username . '】，请及时处理。');
			if ($user1['parent_id'] != 0) {
				$user2 = M('User')->where(['user_id'=>$user1['parent_id']])->find();
				send_sms($user2['phone'], '【' . $danger_name .'】风险点存在重要安全隐患，上报人【' . $username . '】，请及时处理。');
				if ($user2['parent_id'] != 0) {
					$user3 = M('User')->where(['user_id'=>$user2['parent_id']])->find();
					send_sms($user3['phone'], '【' . $danger_name .'】风险点存在重要安全隐患，上报人【' . $username . '】，请及时处理。');
					if ($user3['parent_id'] != 0) {
						$user4 = M('User')->where(['user_id'=>$user3['parent_id']])->find();
						send_sms($user4['phone'], '【' . $danger_name .'】风险点存在重要安全隐患，上报人【' . $username . '】，请及时处理。');
					}
				}
			}
		}
		$this->ajaxReturn(['status' => 1, 'msg' => 'success']);
	}
}
